/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pass_client.admin;

import entities.Assignment;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.*;
import pasremote.PASRemote;

/**
 *
 * @author Administrator
 */
public class ProblemFrame extends javax.swing.JFrame {

    private Long id;
    private final PASRemote bean;

    /**
     * Creates new form ProblemFrame
     */
    ProblemFrame(PASRemote bean, Long id) {
        this.bean = bean;
        this.id = id;
        initComponents();
        initUserComponents();

        if (id != -1) {
            List<Assignment> assignments = bean.getAssignments();
            Assignment assignment = null;
            for (Assignment s : assignments) {
                if (s.getId().equals(id)) {
                    assignment = s;
                }
            }
            txtAsmtName.setText(assignment.getName());
            txtDescription.setText(assignment.getDescription());
            txtInput.setText(assignment.getTestInputs().get(0));
            txtOutput.setText(assignment.getTestOutputs().get(0));
            datePicker2.setDate(assignment.getEndDate());
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        jPanel7 = new javax.swing.JPanel();
        jPanel8 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtInput = new javax.swing.JTextArea();
        jPanel9 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        txtOutput = new javax.swing.JTextArea();
        jPanel10 = new javax.swing.JPanel();
        jPanel13 = new javax.swing.JPanel();
        txtAsmtName = new javax.swing.JTextField();
        jPanel14 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jPanel15 = new javax.swing.JPanel();
        jPanel16 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        datePicker2 = new pass_client.admin.DatePicker();
        jPanel2 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        btnStatistics = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        btnApply = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Problem Information");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Problem Info"));
        jPanel1.setLayout(new java.awt.BorderLayout());

        jPanel6.setLayout(new java.awt.GridLayout(1, 2));

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Description"));
        jPanel5.setLayout(new java.awt.BorderLayout());

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        jScrollPane1.setViewportView(txtDescription);

        jPanel5.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        jPanel6.add(jPanel5);

        jPanel7.setLayout(new java.awt.GridLayout(2, 0));

        jPanel8.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Input ", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
        jPanel8.setLayout(new java.awt.BorderLayout());

        txtInput.setColumns(20);
        txtInput.setRows(5);
        jScrollPane2.setViewportView(txtInput);

        jPanel8.add(jScrollPane2, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel8);

        jPanel9.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Output", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 0, 0)));
        jPanel9.setLayout(new java.awt.BorderLayout());

        txtOutput.setColumns(20);
        txtOutput.setRows(5);
        jScrollPane3.setViewportView(txtOutput);

        jPanel9.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel7.add(jPanel9);

        jPanel6.add(jPanel7);

        jPanel1.add(jPanel6, java.awt.BorderLayout.CENTER);

        jPanel10.setLayout(new java.awt.BorderLayout());

        jPanel13.setLayout(new java.awt.BorderLayout());

        txtAsmtName.setColumns(22);
        jPanel13.add(txtAsmtName, java.awt.BorderLayout.EAST);

        jPanel14.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel2.setText("Assignment Name");
        jPanel14.add(jLabel2);

        jPanel13.add(jPanel14, java.awt.BorderLayout.CENTER);

        jPanel10.add(jPanel13, java.awt.BorderLayout.CENTER);

        jPanel15.setLayout(new java.awt.BorderLayout());

        jPanel16.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.LEFT));

        jLabel3.setText("Deadline");
        jPanel16.add(jLabel3);

        jPanel15.add(jPanel16, java.awt.BorderLayout.CENTER);
        jPanel15.add(datePicker2, java.awt.BorderLayout.EAST);

        jPanel10.add(jPanel15, java.awt.BorderLayout.SOUTH);

        jPanel1.add(jPanel10, java.awt.BorderLayout.NORTH);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setLayout(new java.awt.BorderLayout());

        btnStatistics.setText("Statistics");
        btnStatistics.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStatisticsActionPerformed(evt);
            }
        });
        jPanel3.add(btnStatistics);

        jPanel2.add(jPanel3, java.awt.BorderLayout.WEST);

        btnApply.setText("Apply");
        btnApply.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnApplyActionPerformed(evt);
            }
        });
        jPanel4.add(btnApply);

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        jPanel4.add(btnCancel);

        jPanel2.add(jPanel4, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnStatisticsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStatisticsActionPerformed
        LogFrame logFrame = new LogFrame(bean, id);
    }//GEN-LAST:event_btnStatisticsActionPerformed

    private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnApplyActionPerformed
        if (id == -1) {
            Assignment asmt = new Assignment();
            asmt.setEndDate(datePicker2.getDate());
            asmt.setDescription(txtDescription.getText());
            asmt.setName(txtAsmtName.getText());
            asmt.setStartDate(new Date(System.currentTimeMillis()));
            ArrayList<String> arr = new ArrayList<String>();
            arr.add(txtInput.getText());
            asmt.setTestInputs(arr);
            ArrayList<String> arr2 = new ArrayList<String>();
            arr2.add(txtOutput.getText());
            asmt.setTestOutputs(arr2);
            if (bean.createAssignment(asmt)) {
                JOptionPane.showMessageDialog(this, "Create new Assignment!");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "already exist assignment name!");
            }
        } else {
            Assignment asmt = new Assignment();
            asmt.setId(id);
            asmt.setEndDate(datePicker2.getDate());
            asmt.setDescription(txtDescription.getText());
            asmt.setName(txtAsmtName.getText());
            asmt.setStartDate(new Date(System.currentTimeMillis()));
            ArrayList<String> arr = new ArrayList<String>();
            arr.add(txtInput.getText());
            asmt.setTestInputs(arr);
            ArrayList<String> arr2 = new ArrayList<String>();
            arr2.add(txtOutput.getText());
            asmt.setTestOutputs(arr2);
            if (bean.modifyAssignment(asmt)) {
                JOptionPane.showMessageDialog(this, "Modify this Assignment!");
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(this, "Connection Error, try again");
            }
        }

    }//GEN-LAST:event_btnApplyActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnApply;
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnStatistics;
    private pass_client.admin.DatePicker datePicker2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel10;
    private javax.swing.JPanel jPanel13;
    private javax.swing.JPanel jPanel14;
    private javax.swing.JPanel jPanel15;
    private javax.swing.JPanel jPanel16;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JPanel jPanel7;
    private javax.swing.JPanel jPanel8;
    private javax.swing.JPanel jPanel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextField txtAsmtName;
    private javax.swing.JTextArea txtDescription;
    private javax.swing.JTextArea txtInput;
    private javax.swing.JTextArea txtOutput;
    // End of variables declaration//GEN-END:variables

    private void initUserComponents() {
        setLocationByPlatform(true);
        setSize(500, 500);
        setVisible(true);
    }
}
